package controller;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import view.Canvas;
import view.Designer;
import model.Prescription;
import model.PrescriptionObject;
/** * An event handler class for the motions the mouse has upon the canvas on the designer * @author Daniel Bond, Mohammed Abdullah, Gideon Pyzer * */
public class CanvasMouseMotionListener extends MouseMotionAdapter{	
	/**
	 * When the mouse is dragged across the canvas. This will set
	 * the prescription to changed. It is mainly used when resizing
	 * a component or moving.
	 */
	public void mouseDragged(MouseEvent e){
		Canvas canvas = Canvas.getInstance();
		Designer designer = Designer.getInstance();
		Prescription prescription = Prescription.getInstance();
		//Check if the canvas is in a dragging state
		if(canvas.isDrag()){					
			//Record the change in x and y
			int changeX = e.getX() - canvas.getStartX();
			int changeY = e.getY() - canvas.getStartY();
						//Set the change in x and y to the canvas			//canvas.setDraggedX(canvas.getCurrentX() + changeX);			canvas.setDraggedY(canvas.getCurrentY() + changeY);			canvas.setDraggedX(canvas.getCurrentX() + changeX);			//canvas.checkBoundaries(); <-- uncomment this for snapping functionality
			prescription.setChanged();
		}
		//Check if the canvas is in a resizing state
		else if(canvas.isResize()){
			//Set the canvas resize points to current x and y
			canvas.setCurrentCoordinates(e.getX(), e.getY());
			prescription.setChanged();
		}
		
		//Update the View and Models after each motion
		
		designer.update();
		canvas.repaint();
		
			
	}
	
	/**
	 * This is called when the mouse is moved, without clicking. 
	 * This would invoke the hover-over state of the object under the
	 * mouse.
	 */
	public void mouseMoved(MouseEvent e){
		
		Prescription prescription = Prescription.getInstance();
		Canvas canvas = Canvas.getInstance();
		Designer designer = Designer.getInstance();
		
		//Iterate through the objects on the canvas
		for(int n = prescription.objects().size() - 1; n >=0; n--){
		PrescriptionObject object = prescription.objects().get(n);
		
			//If object is under mouse coordinates
			if(object.checkBounds(e.getX(), e.getY())){
				
				//Can't set a selected object to selected!!
				if(!object.isSelected()){
					object.setHoverOver(true);
					canvas.repaint();
				}
			}
			else{
				object.setHoverOver(false);
				canvas.repaint();
			}
		}
		
		//Set the coordinates to coordinate display at the bottom
		designer.setCoords("x:" + e.getX() + " y:" + e.getY());

	}
	
}